#loading eviction data

evictions<- read.csv("Evictions.csv")
#exclude commercial
evictions<- evictions[evictions$Residential.Commercial=="Residential",]
#order by date
evictions<- evictions[order(as.Date(evictions$Executed.Date, format="%m/%d/%Y")),]
myvars<- c("Executed.Date","BOROUGH", "Latitude","Longitude", "Community.Board")
new_evictions<- evictions[myvars]


##WEEKLY EVICTIONS
weekly<- read.csv("newyork_weekly_percent_change.csv")
#exclude geoID, include only zip code
weekly<- weekly[weekly$type=="Zip Code",]
zipcodes<- c("10025", "10026", "10027", "10030", "10031")
class(weekly$GEOID)
## [1] "character"
cb9<- weekly[weekly$GEOID %in% zipcodes,]
cb9<- cb9[order(as.Date(cb9$week_date, format="%m/%d/%Y")),]
write.csv(cb9, "cb9.csv")

bronx<- c(10463, 10471, 10466, 10469, 10470, 10475, 10458, 10467, 10468, 10461, 10462, 10464, 10465, 10472, 10473,  10453, 10457, 10460, 10451, 10452, 10456, 10454, 10455, 10459, 10474 )
brooklyn<- c(11211, 11222, 11201, 11205, 11215, 11217, 11231,11213, 11212, 11216, 11233, 11238,11207, 11208, 11220, 11232,11204, 11218, 11219, 11230, 11203, 11210, 11225, 11226, 11234, 11236, 11239,  11209, 11214, 11228, 11223, 11224, 11229, 11235,11206, 11221, 11237 )
manhattan<-c(10031, 10032, 10033, 10034, 10040,10026, 10027, 10030, 10037, 10039,10029, 10035, 10023, 10024, 10025, 10021, 10028, 10044, 10128,10001, 10011, 10018, 10019, 10020, 10036, 10010, 10016, 10017, 10022,10012, 10013, 10014, 10002, 10003, 10009, 10004, 10005, 10006, 10007, 10038, 10280)
queens<-c(11101, 11102, 11103, 11104, 11105, 11106, 11368, 11369, 11370, 11372, 11373, 11377, 11378,  11354, 11355, 11356, 11357, 11358, 11359, 11360,11361, 11362, 11363, 11364,  11374, 11375, 11379, 11385,  11365, 11366, 11367,11414, 11415, 11416, 11417, 11418, 11419, 11420, 11421,  11412, 11423, 11432, 11433, 11434, 11435, 11436, 11004, 11005, 11411, 11413, 11422, 11426, 11427, 11428, 11429,  11691, 11692, 11693, 11694, 11695, 11697  )
staten<- c( 10302, 10303, 10310, 10301, 10304, 10305,10314, 10306, 10307, 10308, 10309, 10312)

bronx<- as.character(bronx)
brooklyn<- as.character(brooklyn)
manhattan<-as.character(manhattan)
queens<-as.character(queens)
staten<- as.character(staten)

nyc<- c(10463, 10471, 10466, 10469, 10470, 10475, 10458, 10467, 10468, 10461, 10462, 10464, 10465, 10472, 10473,  10453, 10457, 10460, 10451, 10452, 10456, 10454, 10455, 10459, 10474,11211, 11222, 11201, 11205, 11215, 11217, 11231,11213, 11212, 11216, 11233, 11238,11207, 11208, 11220, 11232,11204, 11218, 11219, 11230, 11203, 11210, 11225, 11226, 11234, 11236, 11239,  11209, 11214, 11228, 11223, 11224, 11229, 11235,11206, 11221, 11237,10031, 10032, 10033, 10034, 10040,10026, 10027, 10030, 10037, 10039,10029, 10035, 10023, 10024, 10025, 10021, 10028, 10044, 10128,10001, 10011, 10018, 10019, 10020, 10036, 10010, 10016, 10017, 10022,10012, 10013, 10014, 10002, 10003, 10009, 10004, 10005, 10006, 10007, 10038, 10280,11101, 11102, 11103, 11104, 11105, 11106, 11368, 11369, 11370, 11372, 11373, 11377, 11378,  11354, 11355, 11356, 11357, 11358, 11359, 11360,11361, 11362, 11363, 11364,  11374, 11375, 11379, 11385,  11365, 11366, 11367,11414, 11415, 11416, 11417, 11418, 11419, 11420, 11421,  11412, 11423, 11432, 11433, 11434, 11435, 11436, 11004, 11005, 11411, 11413, 11422, 11426, 11427, 11428, 11429,  11691, 11692, 11693, 11694, 11695, 11697, 10302, 10303, 10310, 10301, 10304, 10305,10314, 10306, 10307, 10308, 10309, 10312)
nyc<-as.character(nyc)

bronx<- weekly[weekly$GEOID %in% bronx,]
bronx<- bronx[order(as.Date(bronx$week_date, format="%m/%d/%Y")),]
brooklyn<-weekly[weekly$GEOID %in% brooklyn,]
brooklyn<- brooklyn[order(as.Date(brooklyn$week_date, format="%m/%d/%Y")),]
manhattan<-weekly[weekly$GEOID %in% manhattan,]
manhattan<- manhattan[order(as.Date(manhattan$week_date, format="%m/%d/%Y")),]
queens<-weekly[weekly$GEOID %in% queens,]
queens<- queens[order(as.Date(queens$week_date, format="%m/%d/%Y")),]
staten<-weekly[weekly$GEOID %in% staten,]
staten<- staten[order(as.Date(staten$week_date, format="%m/%d/%Y")),]

nyc<- weekly[weekly$GEOID %in% nyc,]
nyc<- nyc[order(as.Date(nyc$week_date, format="%m/%d/%Y")),]

bronx$filings_2020<-as.numeric(bronx$filings_2020)
bronx_evictions = aggregate(filings_2020 ~ week, bronx, sum)
brooklyn$filings_2020<-as.numeric(brooklyn$filings_2020)
brooklyn_evictions = aggregate(filings_2020 ~ week, brooklyn, sum)
manhattan$filings_2020<-as.numeric(manhattan$filings_2020)
manhattan_evictions = aggregate(filings_2020 ~ week, manhattan, sum)
queens$filings_2020<-as.numeric(queens$filings_2020)
queens_evictions = aggregate(filings_2020 ~ week, queens, sum)
staten$filings_2020<-as.numeric(staten$filings_2020)
staten_evictions = aggregate(filings_2020 ~ week, staten, sum)
nyc$filings_2020<-as.numeric(nyc$filings_2020)
nyc_evictions = aggregate(filings_2020 ~ week, nyc, sum)


fivenum(bronx_evictions$filings_2020)
## [1]    0  139  355  558 1421
hist(bronx_evictions$filings_2020)

fivenum(brooklyn_evictions$filings_2020)
## [1]    0.0  144.0  240.5  416.0 1080.0
hist(brooklyn_evictions$filings_2020)

fivenum(manhattan_evictions$filings_2020)
## [1]   0.0 102.0 184.5 303.0 796.0
hist(manhattan_evictions$filings_2020)

fivenum(queens_evictions$filings_2020)
## [1]   0.0 100.0 191.5 294.0 686.0
hist(queens_evictions$filings_2020)

fivenum(staten_evictions$filings_2020)
## [1]   0.0   8.0  18.5  36.0 148.0
hist(staten_evictions$filings_2020)

fivenum(nyc_evictions$filings_2020)
## [1]    0.0  562.0  988.5 1632.0 3722.0
hist(nyc_evictions$filings_2020)

#creating timeseries with plotly

#creating variable that sums evictions for 5 zip codes
cb9$filings_2020<-as.numeric(cb9$filings_2020)
sum_evictions = aggregate(filings_2020 ~ week, cb9, sum)

# Usual area chart
#cb9$week_date <- as.Date(cb9$week_date)
#p <- cb9 %>%
 # ggplot( aes(x=week_date, y=filings_avg)) +
  #  geom_area(fill="#69b3a2", alpha=0.5) +
  #  geom_line(color="#69b3a2") +
   # ylab("total number of evictions filings") +
   # ylim(c(0,100))+
   # theme_ipsum()

# Turn it interactive with ggplotly
#p <- ggplotly(p)
#p


# cb9
p <- sum_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings") +
    ylim(c(0,100))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
## Warning: Removed 15 rows containing missing values (position_stack).
p
#bronx
p <- bronx_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings in bronx") +
    ylim(c(0,1500))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#brooklyn
p <- brooklyn_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings in brooklyn") +
    ylim(c(0,1100))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#manhattan
p <- manhattan_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings in manhattan") +
    ylim(c(0,800))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#queens
p <- queens_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings in queens") +
    ylim(c(0,700))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#staten island
p <- staten_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings in staten island") +
    ylim(c(0,150))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#nyc
p <- nyc_evictions %>%
  ggplot( aes(x=week ,y=filings_2020)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings in all nyc") +
    ylim(c(0,4000))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#saving new csv with boroughs and weeks
df<-cbind(sum_evictions, bronx_evictions, brooklyn_evictions, manhattan_evictions, queens_evictions, staten_evictions, nyc_evictions)
names(df)[2]<-'cb9_filings'
names(df)[4]<-'bronx_filings'
names(df)[6]<-'brooklyn_filings'
names(df)[8]<-'manhattan_filings'
names(df)[10]<-'queens_filings'
names(df)[12]<-'staten_filings'
names(df)[14]<-'nyc_filings'
df<- df[,c(1,2,4,6,8,10,12,14)]
write.csv(df, "boroughs_evictions")

##figuring out which zip codes are in which cb

#plot cbs
cb_boundaries<-st_read("nycdta2020_22a/nycdta2020.shp")
## Reading layer `nycdta2020' from data source 
##   `/Users/adina/Desktop/spec/nycdta2020_22a/nycdta2020.shp' using driver `ESRI Shapefile'
## Simple feature collection with 71 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913175.1 ymin: 120128.4 xmax: 1067383 ymax: 272844.3
## Projected CRS: NAD83 / New York Long Island (ftUS)
manhattan_cb<-cb_boundaries[cb_boundaries$BoroName=="Manhattan",]
tm_shape(cb_boundaries) +
    tm_polygons()

tm_shape(manhattan_cb)+tm_polygons()

#plot zip code
zip_boundaries<-st_read("ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/adina/Desktop/spec/ZIP_CODE_040114/ZIP_CODE_040114.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 263 features and 12 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
manhattan_zip<-zip_boundaries[zip_boundaries$CTY_FIPS=="061",]
tm_shape(manhattan_zip)+tm_polygons()

geo_join <- st_join(manhattan_zip, manhattan_cb, left = FALSE, largest = TRUE)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
write.csv(geo_join, "manhattan_zip_with_cb.csv")
geo_join<-geo_join[!duplicated(geo_join$ZIPCODE),]

names(weekly)[names(weekly) == 'GEOID'] <- "ZIPCODE"

cb_1<- geo_join[geo_join$CDTA2020=="MN01",]
cb_1$ZIPCODE
##  [1] "10048" "10013" "10007" "10038" "10006" "10005" "10004" "10280" "10281"
## [10] "10282" "10279" "10270" "10271" "10041" "10278" "10043" "10081" "10275"
## [19] "10265" "10045" "10080" "10203" "10259" "10260" "10285" "10286"
cb1zip<-cb_1$ZIPCODE
evictionscb1<- weekly[weekly$ZIPCODE %in% cb1zip,]
evictionscb1<- evictionscb1[order(as.Date(evictionscb1$week_date, format="%m/%d/%Y")),]
cb_2<- geo_join[geo_join$CDTA2020=="MN02",]
cb_2$ZIPCODE
## [1] "10014" "10012"
cb2zip<-cb_2$ZIPCODE
evictionscb2<- weekly[weekly$ZIPCODE %in% cb2zip,]
evictionscb2<- evictionscb2[order(as.Date(evictionscb2$week_date, format="%m/%d/%Y")),]
cb_3<- geo_join[geo_join$CDTA2020=="MN03",]
cb_3$ZIPCODE
## [1] "10003" "10002" "10009"
cb3zip<-cb_3$ZIPCODE
evictionscb3<- weekly[weekly$ZIPCODE %in% cb3zip,]
evictionscb3<- evictionscb3[order(as.Date(evictionscb3$week_date, format="%m/%d/%Y")),]
cb_4<- geo_join[geo_join$CDTA2020=="MN04",]
cb_4$ZIPCODE
## [1] "10018" "10001" "10011" "10019" "10036"
cb4zip<-cb_4$ZIPCODE
evictionscb4<- weekly[weekly$ZIPCODE %in% cb4zip,]
evictionscb4<- evictionscb4[order(as.Date(evictionscb4$week_date, format="%m/%d/%Y")),]
cb_5<- geo_join[geo_join$CDTA2020=="MN05",]
cb_5$ZIPCODE
##  [1] "10020" "10055" "10111" "10153" "10165" "10105" "10118" "10176" "10112"
## [10] "10122" "10107" "10103" "10177" "10104" "10110" "10175" "10151" "10173"
## [19] "10121" "10123" "10106" "10120" "10096" "10097" "10196" "10119"
cb5zip<-cb_5$ZIPCODE
evictionscb5<- weekly[weekly$ZIPCODE %in% cb5zip,]
evictionscb5<- evictionscb5[order(as.Date(evictionscb5$week_date, format="%m/%d/%Y")),]
cb_6<- geo_join[geo_join$CDTA2020=="MN06",]
cb_6$ZIPCODE
##  [1] "10017" "10016" "10010" "10154" "10152" "10022" "10168" "10170" "10174"
## [10] "10166" "10169" "10167" "10172" "10171" "10178" "10158" "10155" "10047"
cb6zip<-cb_6$ZIPCODE
evictionscb6<- weekly[weekly$ZIPCODE %in% cb6zip,]
evictionscb6<- evictionscb6[order(as.Date(evictionscb6$week_date, format="%m/%d/%Y")),]
cb_7<- geo_join[geo_join$CDTA2020=="MN07",]
cb_7$ZIPCODE
## [1] "10024" "10025" "10023" "10069"
cb7zip<-cb_7$ZIPCODE
evictionscb7<- weekly[weekly$ZIPCODE %in% cb7zip,]
evictionscb7<- evictionscb7[order(as.Date(evictionscb7$week_date, format="%m/%d/%Y")),]
cb_8<- geo_join[geo_join$CDTA2020=="MN08",]
cb_8$ZIPCODE
## [1] "10128" "10028" "10021" "10044" "10065" "10075" "10162"
cb8zip<-cb_8$ZIPCODE
evictionscb8<- weekly[weekly$ZIPCODE %in% cb8zip,]
evictionscb8<- evictionscb8[order(as.Date(evictionscb8$week_date, format="%m/%d/%Y")),]
cb_9<- geo_join[geo_join$CDTA2020=="MN09",]
cb_9$ZIPCODE
## [1] "10031" "10027" "10115"
cb9zip<-cb_9$ZIPCODE
evictionscb9<- weekly[weekly$ZIPCODE %in% cb9zip,]
evictionscb9<- evictionscb9[order(as.Date(evictionscb9$week_date, format="%m/%d/%Y")),]
cb_10<- geo_join[geo_join$CDTA2020=="MN10",]
cb_10$ZIPCODE
## [1] "10039" "10030" "10037" "10026"
cb10zip<-cb_10$ZIPCODE
evictionscb10<- weekly[weekly$ZIPCODE %in% cb10zip,]
evictionscb10<- evictionscb10[order(as.Date(evictionscb10$week_date, format="%m/%d/%Y")),]
cb_11<- geo_join[geo_join$CDTA2020=="MN11",]
cb_11$ZIPCODE
## [1] "10035" "10029"
cb11zip<-cb_11$ZIPCODE
evictionscb11<- weekly[weekly$ZIPCODE %in% cb11zip,]
evictionscb11<- evictionscb11[order(as.Date(evictionscb11$week_date, format="%m/%d/%Y")),]
cb_12<- geo_join[geo_join$CDTA2020=="MN12",]
cb_12$ZIPCODE
## [1] "10034" "10033" "10040" "10032"
cb12zip<-cb_12$ZIPCODE
evictionscb12<- weekly[weekly$ZIPCODE %in% cb12zip,]
evictionscb12<- evictionscb12[order(as.Date(evictionscb12$week_date, format="%m/%d/%Y")),]

##plots for each manhattan cb

fivenum(evictionscb1$filings_2020)
## [1]  0  0  0  0 67
fivenum(evictionscb2$filings_2020)
## [1]  0.0  0.0  1.5  3.0 16.0
fivenum(evictionscb3$filings_2020)
## [1]  0  1  3  6 82
fivenum(evictionscb4$filings_2020)
## [1]  0  0  3  6 51
fivenum(evictionscb5$filings_2020)
## [1] 0 0 0 0 0
fivenum(evictionscb6$filings_2020)
## [1]  0  0  0  0 21
fivenum(evictionscb7$filings_2020)
## [1]  0  0  2  6 52
fivenum(evictionscb8$filings_2020)
## [1]  0  0  1  3 39
fivenum(evictionscb10$filings_2020)
## [1]  0  1  5 12 85
fivenum(evictionscb11$filings_2020)
## [1]   0   1   5  12 149
fivenum(evictionscb12$filings_2020)
## [1]  0  3  9 17 69
#cb1
p <- evictionscb1 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb1") +
    ylim(c(0,100))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb2
p <- evictionscb2 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb2") +
    ylim(c(0,50))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb3
p <- evictionscb3 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb3") +
    ylim(c(0,100))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb4
p <- evictionscb4 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb4") +
    ylim(c(0,60))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb5
p <- evictionscb5 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb5") +
    ylim(c(0,10))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb6
p <- evictionscb6 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb6") +
    ylim(c(0,30))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb7
p <- evictionscb7 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb7") +
    ylim(c(0,60))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb8
p <- evictionscb8 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb8") +
    ylim(c(0,50))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb10
p <- evictionscb10 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb10") +
    ylim(c(0,100))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb11
p <- evictionscb11 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb11") +
    ylim(c(0,150))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p
#cb12
p <- evictionscb12 %>%
  ggplot( aes(x=week ,y=filings_2020)) +
   
    geom_line(color="#69b3a2") +
    ylab("total number of evictions filings cb12") +
    ylim(c(0,70))+
    theme_ipsum()

# Turn it interactive with ggplotly
p <- ggplotly(p)
p

##making plot of max evictions in cb,

maxevictions<-c(67,16.0, 82, 51, 0, 21, 52, 39, 96, 85, 149, 69,0)
manhattan_cb$maxevictions<-maxevictions
tm<-tm_shape(manhattan_cb)+tm_polygons("maxevictions")
#tmap_save(tm, filename = "maxevictions.html")
tm

#calculate evictions as 1) percentage of total population and 2) percentage of population below poverty line

pop_2020<-c(78390, 92445, 163141, 131351, 63600, 155614, 222129, 231983, 110458, 130440, 125771, 180206,0)
pov_2019<-c(.076,.076, .22, .112,.112,.087,.095,.061,.199, .205,.234,.182,0)
manhattan_cb$pop<-pop_2020
manhattan_cb$standardevictions<-maxevictions/pop_2020
manhattan_cb$poverty<-pov_2019
manhattan_cb$poppoverty<-pop_2020*pov_2019
manhattan_cb$povevictions<-maxevictions/manhattan_cb$poppoverty
tm1<-tm_shape(manhattan_cb)+tm_polygons("standardevictions")
tm1
## Some legend labels were too wide. These labels have been resized to 0.57, 0.57, 0.57, 0.57, 0.57, 0.57. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

tm2<-tm_shape(manhattan_cb)+tm_polygons("poppoverty", palette = "BuPu",style = "sd")
tm2
## Some legend labels were too wide. These labels have been resized to 0.61, 0.57, 0.57, 0.57, 0.57, 0.57. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

tm3<-tm_shape(manhattan_cb)+tm_polygons("povevictions",palette = "BuPu",style = "sd")
tm3
## Some legend labels were too wide. These labels have been resized to 0.63. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

#January 15, 2022
#corresponds to 108-118
#post moratorium cb data:
morcb1<-evictionscb1[evictionscb1$week %in% 108:118,]
morcb2<-evictionscb2[evictionscb2$week %in% 108:118,]
morcb3<-evictionscb3[evictionscb3$week %in% 108:118,]
morcb4<-evictionscb4[evictionscb4$week %in% 108:118,]
morcb5<-evictionscb5[evictionscb5$week %in% 108:118,]
morcb6<-evictionscb6[evictionscb6$week %in% 108:118,]
morcb7<-evictionscb7[evictionscb7$week %in% 108:118,]
morcb8<-evictionscb8[evictionscb8$week %in% 108:118,]
morcb9<-evictionscb9[evictionscb9$week %in% 108:118,]
morcb10<-evictionscb10[evictionscb10$week %in% 108:118,]
morcb11<-evictionscb11[evictionscb11$week %in% 108:118,]
morcb12<-evictionscb12[evictionscb12$week %in% 108:118,]

mean_mor<-c(mean(morcb1$filings_2020),mean(morcb2$filings_2020),mean(morcb3$filings_2020),mean(morcb4$filings_2020),mean(morcb5$filings_2020),mean(morcb6$filings_2020),mean(morcb7$filings_2020),mean(morcb8$filings_2020),mean(morcb9$filings_2020),mean(morcb10$filings_2020),mean(morcb11$filings_2020),mean(morcb12$filings_2020),0)
mean_mor
##  [1]  0.5363636  3.0454545  6.1212121  7.4181818  0.0000000  0.7967914
##  [7]  5.9318182  3.1428571 12.0606061 11.6363636 10.6363636 16.3636364
## [13]  0.0000000
#average weekly evictions since moratorium ended, standardized by pov population per 100k people
manhattan_cb$povevictions<-mean_mor/manhattan_cb$poppoverty *100000
tm3<-tm_shape(manhattan_cb)+tm_polygons("povevictions",palette = "BuPu", legend.hist = TRUE)+tm_layout(legend.outside = TRUE) 
tm3

tmap_save(tm3,"moratoriummap.svg")
## Map saved to /Users/adina/Desktop/spec/moratoriummap.svg
## Size: 5.106103 by 9.59636 inches
#cd5 has negative value?
#tmaptools::palette_explorer()

#percent change?

cb9$percent_change<-as.numeric(cb9$percent_change)
cb9_change = aggregate(percent_change ~ week, cb9, mean)

manhattan$percent_change<-as.numeric(manhattan$percent_change)
manhattan_change = aggregate(percent_change ~ week, manhattan, mean)

nyc$percent_change<-as.numeric(nyc$percent_change)
nyc_change = aggregate(percent_change ~ week, nyc, mean)

# cb9
p <- cb9_change %>%
  ggplot( aes(x=week ,y=percent_change)) +
    geom_line(color="#69b3a2") +
    ylab("cb9 percent change in evictions comp. to last 4 weeks") +
    ylim(c(-1,10))+
    theme_ipsum()
p

p <- ggplotly(p)
p
#week 61 has high value
check<- cb9[cb9$week==61,]
#zipcode    10030 has percent change of 26.2000000


#ggsave(file="cb9change.svg", p)

#manhattan
fivenum(manhattan_change$percent_change)
## [1] -0.95121951 -0.02190917  0.19455736  0.58542650  3.07560976
p <- manhattan_change %>%
  ggplot( aes(x=week ,y=percent_change)) +
    geom_line(color="#69b3a2") +
    ylab("manhattan percent change in evictions comp. to last 4 weeks") +
    ylim(c(-1,4))+
    theme_ipsum()
p

ggsave(file="manhattanchange.svg", p)
## Saving 7 x 5 in image
q <- ggplotly(p)
q
#nyc
fivenum(nyc_change$percent_change)
## [1] -0.9714286  0.0000000  0.2705943  0.5167089  4.3235167
p <- nyc_change %>%
  ggplot( aes(x=week,y=percent_change)) +
    geom_line(color="#69b3a2") +
    ylab("nyc percent change in evictions comp. to last 4 weeks") +
    ylim(c(-1,4.5))+
    theme_ipsum()

q <- ggplotly(p)
q
p

ggsave(file="nycchange.svg", p)
## Saving 7 x 5 in image